<?php require_once("admin-header.php");?>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Add a contest</title>


<link rel="stylesheet" href="../kindeditor/themes/default/default.css" />
<script charset="utf-8" src="../kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="../kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript">
KindEditor.ready(function(K){
K.create('textarea[name="description"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="input"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="output"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
K.create('textarea[name="hint"]',{
uploadJson : '../upload_json.php',
fileManagerJson : '../file_manager_json.php',
allowFileManager: true,
});
});


</script>
<style type="text/css">  
.comments {  
 width:100%; 
 overflow:auto;  
 word-break:break-all;  
}  
</style>

<?php

	require_once("../include/const.inc.php");
$description="";
 if (isset($_POST['syear']))
{
	
	require_once("../include/db_info.inc.php");
	require_once("../include/check_post_key.php");
	
	$starttime=intval($_POST['syear'])."-".intval($_POST['smonth'])."-".intval($_POST['sday'])." ".intval($_POST['shour']).":".intval($_POST['sminute']).":00";
	$endtime=intval($_POST['eyear'])."-".intval($_POST['emonth'])."-".intval($_POST['eday'])." ".intval($_POST['ehour']).":".intval($_POST['eminute']).":00";
	//	echo $starttime;
	//	echo $endtime;

        $title=$_POST['title'];
        $private=$_POST['private'];
        $description=$_POST['description'];
        if (get_magic_quotes_gpc ()){
                $title = stripslashes ($title);
                $private = stripslashes ($private);
                $description = stripslashes ($description);
        }

	$title=mysql_real_escape_string($title);
	$private=mysql_real_escape_string($private);
	$description=mysql_real_escape_string($description);
	

$langmask=4080;
	//echo $langmask;	
	
	$sql="INSERT INTO `contest`(`title`,`start_time`,`end_time`,`private`,`langmask`,`description`)
		VALUES('$title','$starttime','$endtime','$private',$langmask,'$description')";
//	echo $sql;
	mysql_query($sql) or die(mysql_error());
	$cid=mysql_insert_id();
	echo "Add Contest ".$cid;
	$sql="DELETE FROM `contest_problem` WHERE `contest_id`=$cid";
	$plist=trim($_POST['cproblem']);
	$pieces = explode(",",$plist );
	if (count($pieces)>0 && strlen($pieces[0])>0){
		$sql_1="INSERT INTO `contest_problem`(`contest_id`,`problem_id`,`num`) 
			VALUES ('$cid','$pieces[0]',0)";
		for ($i=1;$i<count($pieces);$i++){
			$sql_1=$sql_1.",('$cid','$pieces[$i]',$i)";
		}
		//echo $sql_1;
		mysql_query($sql_1) or die(mysql_error());
		$sql="update `problem` set defunct='N' where `problem_id` in ($plist)";
		mysql_query($sql) or die(mysql_error());
	}
	$sql="DELETE FROM `privilege` WHERE `rightstr`='c$cid'";
	mysql_query($sql);
	$sql="insert into `privilege` (`user_id`,`rightstr`)  values('".$_SESSION['user_id']."','m$cid')";
	mysql_query($sql);
	$_SESSION["m$cid"]=true;
	$pieces = explode("\n", trim($_POST['ulist']));
	if (count($pieces)>0 && strlen($pieces[0])>0){
		$sql_1="INSERT INTO `privilege`(`user_id`,`rightstr`) 
			VALUES ('".trim($pieces[0])."','c$cid')";
		for ($i=1;$i<count($pieces);$i++)
			$sql_1=$sql_1.",('".trim($pieces[$i])."','c$cid')";
		//echo $sql_1;
		mysql_query($sql_1) or die(mysql_error());
	}
	echo "<script>window.location.href=\"contest_list.php\";</script>";
}
else{
	
   if(isset($_GET['cid'])){
		   $cid=intval($_GET['cid']);
		   $sql="select * from contest WHERE `contest_id`='$cid'";
		   $result=mysql_query($sql) or die(mysql_error());
		   $row=mysql_fetch_object($result);
		   $title=$row->title;
		   mysql_free_result($result);
			$plist="";
			$sql="SELECT `problem_id` FROM `contest_problem` WHERE `contest_id`=$cid ORDER BY `num`";
			$result=mysql_query($sql) or die(mysql_error());
			for ($i=mysql_num_rows($result);$i>0;$i--){
				$row=mysql_fetch_row($result);
				$plist=$plist.$row[0];
				if ($i>1) $plist=$plist.',';
			}
			mysql_free_result($result);
   }
else if(isset($_POST['problem2contest'])){
	   $plist="";
	   //echo $_POST['pid'];
	   sort($_POST['pid']);
	   foreach($_POST['pid'] as $i){		    
			if ($plist) 
				$plist.=','.$i;
			else
				$plist=$i;
	   }
}else if(isset($_GET['spid'])){
	require_once("../include/check_get_key.php");
		   $spid=intval($_GET['spid']);
		 
			$plist="";
			$sql="SELECT `problem_id` FROM `problem` WHERE `problem_id`>=$spid ";
			$result=mysql_query($sql) or die(mysql_error());
			for ($i=mysql_num_rows($result);$i>0;$i--){
				$row=mysql_fetch_row($result);
				$plist=$plist.$row[0];
				if ($i>1) $plist=$plist.',';
			}
			mysql_free_result($result);
}  
?>
<div>
	
<form class="form-horizontal col-sm-6 col-sm-offset-2"  method="post">
	
	<div class="form-group">
        <label class="col-sm-5 control-label">标题</label>
        <div class="col-sm-7">
        <input class="form-control" name="title" value="<?php echo isset($title)?$title:""?>" type="text"/></div>
    </div>
	<div class="form-group">
        <label class="col-sm-5 control-label">题目</label>
        <div class="col-sm-7">
        <input class="form-control" name="cproblem" value="<?php echo isset($plist)?$plist:""?>" type="text"/></div>
    </div>
	<div class="form-group">
	<label class="col-sm-5 control-label">类型</label>
	<div class="col-sm-7">
	<select class="form-control" name="private">
       <option value="0">比赛</option>
    </select>
	</div>
	</div>
	
	<p align=left>开始时间:<br>&nbsp;&nbsp;&nbsp;
	Year:<input  class=input-mini type=text name=syear value=<?php echo date('Y')?> size=4 >
	Month:<input class=input-mini  type=text name=smonth value=<?php echo date('m')?> size=2 >
	Day:<input class=input-mini type=text name=sday size=2 value=<?php echo date('d')?> >
	Hour:<input class=input-mini    type=text name=shour size=2 value=<?php echo date('H')?>>
	Minute:<input class=input-mini    type=text name=sminute value=00 size=2 ></p>
	<p align=left>结束时间:<br>&nbsp;&nbsp;&nbsp;
	Year:<input class=input-mini    type=text name=eyear value=<?php echo date('Y')?> size=4 >
	Month:<input class=input-mini    type=text name=emonth value=<?php echo date('m')?> size=2 >
	
	Day:<input class=input-mini  type=text name=eday size=2 value=<?php echo date('d')+(date('H')+4>23?1:0)?>>
	Hour:<input class=input-mini  type=text name=ehour size=2 value=<?php echo (date('H')+4)%24?>>
	Minute:<input class=input-mini  type=text name=eminute value=00 size=2 ></p>
	

	
	<?php require_once("../include/set_post_key.php");?>
	
	<br>
	<p align=left>比赛公告:<br><textarea rows=13 name=description cols=80></textarea>
	
	
	<div class="form-group">
        <div class="col-sm-offset-5 col-sm-7">
            <input type="submit" class="btn btn-primary col-sm-8" value="添加"/>
        </div>
    </div>
	
	</form>
</div>
<?php }

?>

